package com.dissertation.model.auth;

import com.dissertation.model.MongodbBaseModel;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import javax.validation.constraints.NotNull;
import java.util.*;

public class Role  {

    public static final Role TEACHER = new Role();
    public static final Role STUDENT = new Role();
    public static final Role ADMIN = new Role();
    public static final Set<Role> defaultRoles = new HashSet<>( 3 );
    static {

        Permission select = new Permission();
        select.setName( "select" );

        Permission update = new Permission();
        update.setName( "update" );

        Permission insert = new Permission();
        insert.setName( "insert" );

        Permission delete = new Permission();
        delete.setName( "delete" );

        Permission addAnother = new Permission();
        addAnother.setName( "other" );

        TEACHER.addPermissions( select, update, insert );
        ADMIN.addPermissions( select, addAnother, update );
        STUDENT.addPermissions( select, insert, update );

        TEACHER.setName( "teacher" );
        STUDENT.setName( "student" );
        ADMIN.setName( "admin" );

        defaultRoles.add( TEACHER );
        defaultRoles.add( STUDENT );
        defaultRoles.add( ADMIN );
    }


    private String name;

    @NotNull
    private Set<Permission> permissions;

    public Set<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions( Set<Permission> permissions ) {
        this.permissions = permissions;
    }

    public void addPermissions( Permission... p ) {
        permissions = Optional.ofNullable( permissions ).orElse( new HashSet<>() );
        permissions.addAll( Arrays.asList( p ) );
    }

    public String getName() {
        return name;
    }

    public void setName( String name ) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Role role = (Role) o;

        return name != null ? name.equals(role.name) : role.name == null;
    }

    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }
}
